import 'package:flutter/material.dart';

class ScrollControllerDemo extends StatefulWidget {

  const ScrollControllerDemo({super.key});

  @override
  State<StatefulWidget> createState() => ScrollControllerDemoState();

}

class ScrollControllerDemoState extends State<ScrollControllerDemo> {

  final ScrollController _controller = ScrollController();

  var showBtn = false;

   @override
  void initState() {
    super.initState();
    //滚动监听
    _controller.addListener(() {
        print('_controller.offset:${_controller.offset}');
        if (_controller.offset < 1000 && showBtn) {
          setState(() {
             showBtn = false;
          });
        } else if (_controller.offset >= 1000 && !showBtn) {
           setState(() {
              showBtn = true;
           });
        }
    });
  }

   @override
   Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: const Text('滚动控制'),
          backgroundColor: Colors.indigo,
        ),
        body: Scrollbar(
           //隐藏滚动条
           thickness: 0,
           child: ListView.builder(
               itemCount: 50,
               itemExtent: 50,
               controller: _controller,
               itemBuilder: (context,index) {
                 return ListTile(title: Text('item:$index'),);
               }
           ),
        ),
        floatingActionButton: !showBtn ? null : FloatingActionButton(
          child: const Icon(Icons.arrow_upward),
          onPressed: (){
            _controller.animateTo(
                0,
                duration: const Duration(milliseconds: 200),
                curve: Curves.ease);
          }
        ),
      );
   }

   @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

}